package com.xinyue.cross.server.client;

import com.xinyue.game.message.GameServerMessageModel;
import com.xinyue.game.message.GameServerResponseHeader;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.rsocket.netty.NettyRSocketServer;

public class MessageDecodeHandler extends ChannelInboundHandlerAdapter {
    private static Logger logger = LoggerFactory.getLogger(NettyRSocketServer.class);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        int size = byteBuf.readInt();
        GameServerResponseHeader responseHeader = new GameServerResponseHeader();
        responseHeader.read(byteBuf);
        GameServerMessageModel<GameServerResponseHeader> messageModel = new GameServerMessageModel<>();
        messageModel.setHeader(responseHeader);
        messageModel.setBody(byteBuf);
        logger.debug("收到业务服务消息，size: {}, header-> {}", size, responseHeader);
        ctx.fireChannelRead(messageModel);
    }
}
